// 文件上传配置接口
export interface FileUploadConfig {
  accept?: string; // 接受的文件类型，如 "image" 或 "all"
  maxSize?: number; // 最大文件大小（MB）
  maxCount?: number; // 最大文件数量
  uploadUrl?: string; // 上传接口地址
  listType?: "text" | "picture" | "picture-card"; // 显示类型
  tips?: string; // 上传提示信息
}

// 选项数据接口
export interface OptionItem {
  value: string;
  text: string;
}

// 表单字段配置接口
export interface FieldConfig {
  key: string;
  label: string;
  type: "input" | "textarea" | "picker" | "datetime" | "image-upload" | "file-upload" | "readonly";
  placeholder: string;
  required?: boolean;
  inputType?: string;
  maxlength?: number;
  pattern?: string; // 正则表达式验证
  patternErrorMessage?: string; // 正则验证失败的错误信息
  options?: string[];
  optionsData?: OptionItem[]; // 选项数据
  fileConfig?: {
    isIdCard?: boolean;
    idCardType?: "front" | "back";
  }; // 文件上传配置，与XenDynamicDetailsCard组件兼容
  readonly?: boolean; // 只读字段标识
  showInCreate?: boolean; // 是否在新增页面显示
  showInEdit?: boolean; // 是否在编辑页面显示
  showInDetails?: boolean; // 是否在详情页面显示
  formatter?: (value: any) => string; // 字段值格式化函数
  validation?: (
    value: any,
    formData?: any,
  ) => { valid: boolean; message: string } | true;
}

// 表单分组接口
export interface FormSection {
  key: string;
  title: string;
  fields: FieldConfig[];
  isDetailList?: boolean; // 标识是否为明细列表分组
}

// 招聘发布申请表单配置
export const recruitPublishFormConfig: Record<string, FieldConfig> = {
  // 单据信息
  billCode: {
    key: "billCode",
    label: "单据编号",
    type: "readonly",
    placeholder: "系统自动生成",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  billStatus: {
    key: "billStatus",
    label: "单据状态",
    type: "readonly",
    placeholder: "单据状态",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  workflowStatus: {
    key: "workflowStatus",
    label: "工作流状态",
    type: "readonly",
    placeholder: "工作流状态",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  remark: {
    key: "remark",
    label: "备注",
    type: "readonly",
    placeholder: "备注信息",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 基本信息
  jobPosition: {
    key: "jobPosition",
    label: "招聘岗位",
    type: "readonly",
    placeholder: "招聘岗位",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  title: {
    key: "title",
    label: "招聘发布申请",
    type: "readonly",
    placeholder: "招聘发布申请",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  description: {
    key: "description",
    label: "岗位描述",
    type: "readonly",
    placeholder: "岗位描述",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 招聘信息
  hireNumber: {
    key: "hireNumber",
    label: "招聘人数",
    type: "readonly",
    placeholder: "招聘人数",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  expectedDate: {
    key: "expectedDate",
    label: "到岗时间",
    type: "readonly",
    placeholder: "到岗时间",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  recruitRemark: {
    key: "recruitRemark",
    label: "备注",
    type: "readonly",
    placeholder: "招聘备注",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 岗位信息
  educationRequirement: {
    key: "educationRequirement",
    label: "学历要求",
    type: "readonly",
    placeholder: "学历要求",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  majorRequirement: {
    key: "majorRequirement",
    label: "专业要求",
    type: "readonly",
    placeholder: "专业要求",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  genderRequirement: {
    key: "genderRequirement",
    label: "性别要求",
    type: "readonly",
    placeholder: "性别要求",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  ageRequirement: {
    key: "ageRequirement",
    label: "年龄要求",
    type: "readonly",
    placeholder: "年龄要求",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  jobRequirement: {
    key: "jobRequirement",
    label: "岗位要求",
    type: "readonly",
    placeholder: "岗位要求",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  otherRequirement: {
    key: "otherRequirement",
    label: "其他要求",
    type: "readonly",
    placeholder: "其他要求",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 待遇信息
  salaryRange: {
    key: "salaryRange",
    label: "薪资范围",
    type: "readonly",
    placeholder: "薪资范围",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  benefits: {
    key: "benefits",
    label: "福利待遇",
    type: "readonly",
    placeholder: "福利待遇",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 发布信息
  publishStartDate: {
    key: "publishStartDate",
    label: "发布开始时间",
    type: "readonly",
    placeholder: "发布开始时间",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  publishEndDate: {
    key: "publishEndDate",
    label: "发布结束时间",
    type: "readonly",
    placeholder: "发布结束时间",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  isExternalPublish: {
    key: "isExternalPublish",
    label: "是否对外发布",
    type: "readonly",
    placeholder: "是否对外发布",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
    formatter: (value: any) => value ? "是" : "否", // 将 true/false 转换为 是/否
  },

  // 联系信息
  contactPerson: {
    key: "contactPerson",
    label: "联系人",
    type: "readonly",
    placeholder: "联系人",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  contactPhone: {
    key: "contactPhone",
    label: "联系电话",
    type: "readonly",
    placeholder: "联系电话",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  contactEmail: {
    key: "contactEmail",
    label: "联系邮箱",
    type: "readonly",
    placeholder: "联系邮箱",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 流程信息
  approver: {
    key: "approver",
    label: "审批人",
    type: "readonly",
    placeholder: "审批人",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  approvalTime: {
    key: "approvalTime",
    label: "审批时间",
    type: "readonly",
    placeholder: "审批时间",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },

  // 工作信息
  company: {
    key: "company",
    label: "公司",
    type: "readonly",
    placeholder: "公司",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
  group: {
    key: "group",
    label: "所属部门",
    type: "readonly",
    placeholder: "所属部门",
    required: false,
    readonly: true,
    showInCreate: false,
    showInEdit: false,
    showInDetails: true,
  },
};

// 表单分组配置
export const formSections: FormSection[] = [
  {
    key: "billInfo",
    title: "单据信息",
    fields: [
      recruitPublishFormConfig.billCode,
      recruitPublishFormConfig.billStatus,
      recruitPublishFormConfig.workflowStatus,
      recruitPublishFormConfig.remark,
    ],
  },
  {
    key: "basicInfo",
    title: "基本信息",
    fields: [
      recruitPublishFormConfig.jobPosition,
      recruitPublishFormConfig.description,
    ],
  },
  {
    key: "recruitInfo",
    title: "招聘信息",
    fields: [
      recruitPublishFormConfig.hireNumber,
      recruitPublishFormConfig.expectedDate,
      recruitPublishFormConfig.recruitRemark,
    ],
  },
  {
    key: "positionInfo",
    title: "岗位信息",
    fields: [
      recruitPublishFormConfig.educationRequirement,
      recruitPublishFormConfig.majorRequirement,
      recruitPublishFormConfig.genderRequirement,
      recruitPublishFormConfig.ageRequirement,
      recruitPublishFormConfig.jobRequirement,
      recruitPublishFormConfig.otherRequirement,
    ],
  },
  {
    key: "salaryInfo",
    title: "待遇信息",
    fields: [
      recruitPublishFormConfig.salaryRange,
      recruitPublishFormConfig.benefits,
    ],
  },
  {
    key: "publishInfo",
    title: "发布信息",
    fields: [
      recruitPublishFormConfig.publishStartDate,
      recruitPublishFormConfig.publishEndDate,
      recruitPublishFormConfig.isExternalPublish,
    ],
  },
  {
    key: "contactInfo",
    title: "联系信息",
    fields: [
      recruitPublishFormConfig.contactPerson,
      recruitPublishFormConfig.contactPhone,
      recruitPublishFormConfig.contactEmail,
    ],
  },
  {
    key: "processInfo",
    title: "流程信息",
    fields: [
      recruitPublishFormConfig.approver,
      recruitPublishFormConfig.approvalTime,
    ],
  },
  {
    key: "workInfo",
    title: "工作信息",
    fields: [
      recruitPublishFormConfig.company,
      recruitPublishFormConfig.group,
    ],
  },
];

// 根据页面类型过滤表单分组
export const getFormSectionsByPageType = (pageType: 'create' | 'edit' | 'details'): FormSection[] => {
  return formSections.map(section => ({
    ...section,
    fields: section.fields.filter(field => {
      if (pageType === 'create') {
        return field.showInCreate !== false;
      } else if (pageType === 'edit') {
        return field.showInEdit !== false;
      } else if (pageType === 'details') {
        // 详情页根据showInDetails配置显示字段
        return field.showInDetails !== false;
      }
      return false;
    })
  })).filter(section => section.fields.length > 0);
};

// 表单验证函数
export const validateForm = (
  formData: Record<string, any>,
): { valid: boolean; errors: Record<string, string> } => {
  const errors: Record<string, string> = {};
  let valid = true;

  // 遍历所有字段配置
  Object.values(recruitPublishFormConfig).forEach((field) => {
    const value = formData[field.key];

    // 必填字段验证
    if (field.required && !value) {
      errors[field.key] = `请输入${field.label}`;
      valid = false;
      return;
    }

    // 正则表达式验证
    if (field.pattern && value) {
      const pattern = new RegExp(field.pattern);
      if (!pattern.test(value)) {
        errors[field.key] =
          field.patternErrorMessage || `${field.label}格式不正确`;
        valid = false;
        return;
      }
    }

    // 自定义验证
    if (field.validation && value !== undefined) {
      const result = field.validation(value, formData);
      if (result !== true) {
        errors[field.key] = result.message;
        valid = false;
        return;
      }
    }
  });

  return { valid, errors };
};
